class LoginCommon {
    constructor() {
        const params = new URLSearchParams(location.search)
        console.log(params)
        const loginData = params.get('LoginData')
        if (loginData) {
            // 移除url中的loginData参数
            const url = new URL(location.href)
            url.searchParams.delete('LoginData')
            history.replaceState({}, '', url.toString())
            this.Login(loginData)
        }
    }

    async Login(data) {
        try {
            const LoginDataJSON = atob(data)
            const loginData = JSON.parse(LoginDataJSON)
            localStorage.setItem('user_id', loginData.user_id)
            localStorage.setItem('access_token', loginData.access_token)
            localStorage.setItem('platform', loginData.platform)
            localStorage.setItem('platform_user_id', loginData.platform_user_id)

            await window.pvClient.validate()
        } catch (error) {
            console.error('登录失败:', error)
            window.qcNotify.create({
                message: '登录授权失败，请重试',
                color: 'negative',
                position: 'top',
                timeout: 3000,
                icon: 'error'
            })
        }
    }
}
window.LoginCommon = LoginCommon